﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;
using System.Linq.Expressions;
using System.Web.Security;
using AL.Common;
using AL.DAL;
using AL.DAL.Core;
using AL.DAL.Models;


namespace AL.BLL {

    /// <summary>
    /// HH: 个人会员 业务逻辑操作实现类
    /// </summary>
    public class MemberUserService : ServiceBase<MemberUser>, IMemberUserService {
        private readonly IUnitOfWork unitOfWork;
        private readonly IMemberUserRepository MemberUserRepository;
        
        public MemberUserService(IUnitOfWork unitOfWork, IMemberUserRepository MemberUserRepository) {
            //_Provider = provider ?? Membership.Provider;

            this.unitOfWork = unitOfWork;
            this.MemberUserRepository = MemberUserRepository;

            // 注意：一定要注册这个对象，否则分页查询会无效
            _Repository = MemberUserRepository;
        }


        public int MinPasswordLength {
            get {
                return 6;
            }
        }


        #region 　< 查询操作 >

        public IEnumerable<MemberUser> GetMemberUsers() {
            MemberUserRepository.ProxyCreationEnabled = false;
            var MemberUsers = MemberUserRepository.GetAll();
            return MemberUsers;
        }

        public MemberUser GetMemberUser(int memberID) {
            //MemberUserRepository.ProxyCreationEnabled = false;
            var MemberUser = MemberUserRepository.GetById(memberID);
            return MemberUser;
        }


        #endregion


        #region 　< 新增、修改、删除、保存 锁定操作 >

        // 提交保存操作
        public void Save() {
            unitOfWork.Commit();
        }

        // 创建用户
        public int CreateMemberUser(MemberUser MemberUser) {
            MemberUser.CreateDate = DateTime.Now;
            MemberUser.UpdateDate = DateTime.Now;

            MemberUserRepository.Create(MemberUser);
            Save();

            return MemberUser.MemberID;
        }

        // 修改用户
        public bool UpdateMemberUser(MemberUser MemberUser) {
            MemberUserRepository.Update(MemberUser);
            Save();

            return true;
        }

        // 删除用户
        public void DeleteMemberUser(int memberID) {
                var MemberUser = GetMemberUser(memberID);

                MemberUserRepository.Delete(MemberUser);

                Save();
        }

        #endregion


    }






    /// <summary>
    /// HH: 用户角色 业务逻辑操作 接口类
    /// </summary>
    public interface IMemberUserService : IServiceBase<MemberUser> {
        /// <summary>
        /// 最短密码长度
        /// </summary>
        int MinPasswordLength { get; }

        #region 　< 查询操作 >

        /// <summary>
        /// HH: 获取所有用户集合
        /// </summary>
        IEnumerable<MemberUser> GetMemberUsers();
        /// <summary>
        /// HH: 根据用户名获取一个用户对象
        /// </summary>
        MemberUser GetMemberUser(int memberID);

        ///// <summary>
        ///// HH: 用户登录验证
        ///// </summary>
        //bool ValidateMemberUser(string MemberUserName, string password);

        #endregion

        #region 　< 新增、修改、删除、保存 锁定操作 >

        /// <summary>
        /// HH: 新建用户（同时新增相关表记录）
        /// </summary>
        int CreateMemberUser(MemberUser MemberUser);
        ///// <summary>
        ///// HH: 新建用户（同时新增相关表记录）
        ///// </summary>
        //MembershipCreateStatus CreateMemberUser(string MemberUserName, string password, string email);

        /// <summary>
        /// HH: 修改用户
        /// </summary>
        bool UpdateMemberUser(MemberUser MemberUser);
        /// <summary>
        /// HH: 删除用户（级联删除）
        /// </summary>
        void DeleteMemberUser(int memberID);


        /// <summary>
        /// HH: 保存操作
        /// </summary>
        void Save();

        #endregion



    }

}